package com.newcapec.common.utils.iot;

/**
 * Byte工具类
 *
 * @author wangdesen
 */
public class ByteUtil {

    public static int[] subBytes(int[] src, int begin, int count) {
        int[] bs = new int[count];
        for (int i = begin; i < begin + count; i++)
            bs[i - begin] = src[i];
        return bs;
    }

    public static String bytesToHexString(int[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    /**
     * byte[]转十六进制String
     *
     * @param byteArray
     * @return
     */
    public static String byteArrayToHexStr(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[byteArray.length * 2];
        for (int j = 0; j < byteArray.length; j++) {
            int v = byteArray[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    /**
     * 十六进制String转byte[]
     *
     * @param hexString
     * @return
     */
    public static byte[] hexToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }

        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] bytes = new byte[length];
        String hexDigits = "0123456789abcdef";
        for (int i = 0; i < length; i++) {
            int pos = i * 2; // 两个字符对应一个byte
            int h = hexDigits.indexOf(hexChars[pos]) << 4; // 注1
            int l = hexDigits.indexOf(hexChars[pos + 1]); // 注2
            if (h == -1 || l == -1) { // 非16进制字符
                return null;
            }
            bytes[i] = (byte) (h | l);
        }
        return bytes;
    }

    public static byte[] longToByteArray(long s) {
        byte[] targets = new byte[4];
        byte[] temp = new byte[8];
        for (int i = 0; i < 8; i++) {
            int offset = (temp.length - 1 - i) * 8;
            temp[i] = (byte) ((s >>> offset) & 0xff);
        }
        System.arraycopy(temp, 4, targets, 0, 4);

        return targets;
    }

    public static long byteArrayToLong(byte[] original) {
        byte[] temp = new byte[original.length];
        int j = 0;
        for (int i = original.length - 2; i >= 0; i--) {
            temp[j] = original[i];
            j++;
        }

        long target = ((((long) temp[0] & 0xff) << 24)
                | (((long) temp[1] & 0xff) << 16)
                | (((long) temp[2] & 0xff) << 8) | (((long) temp[3] & 0xff) << 0));


        return target;
    }

    /***
     * byte转16进制  生成Scardsnr使用
     * @param byteArray
     * @param begin
     * @param len
     * @param lenFillSpace
     * @return
     */
    public static String ByteArrayToHexStr(byte[] byteArray, int begin, int len, byte lenFillSpace) {
        StringBuilder stringBuilder = new StringBuilder("");

        for (int i = 0, j = begin; i < len; i++, j++) {
            int v = byteArray[j] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);

            if (i > 0 && lenFillSpace > 0 && (i % lenFillSpace == 0))
                stringBuilder.append(" ");
        }

        return stringBuilder.toString();
    }

    public static byte[] subToBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i = begin; i < begin + count; i++)
            bs[i - begin] = src[i];
        return bs;
    }

    /**
     * 将一个char字符转换成一个字节类型
     *
     * @param c
     * @return
     */
    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (toByte(hexChars[pos]) << 4 | toByte(hexChars[pos + 1]));

            String hex = Integer.toHexString(d[i] & 0xFF);
        }

        return d;
    }

    public static byte[] longToBytes(long l) {
        byte[] b = new byte[8];
        b[0] = (byte) (l >>> 56);
        b[1] = (byte) (l >>> 48);
        b[2] = (byte) (l >>> 40);
        b[3] = (byte) (l >>> 32);
        b[4] = (byte) (l >>> 24);
        b[5] = (byte) (l >>> 16);
        b[6] = (byte) (l >>> 8);
        b[7] = (byte) (l);
        return b;
    }


    public static byte[] intToByteArray(int a) {
        return new byte[]{
                (byte) ((a >> 24) & 0xFF),
                (byte) ((a >> 16) & 0xFF),
                (byte) ((a >> 8) & 0xFF),
                (byte) (a & 0xFF)
        };
    }

    public static byte[] longTo5Bytes(long l) {
        byte[] b = new byte[5];

        b[0] = (byte) (l >>> 32);
        b[1] = (byte) (l >>> 24);
        b[2] = (byte) (l >>> 16);
        b[3] = (byte) (l >>> 8);
        b[4] = (byte) (l);
        return b;
    }
}
